<?php 
Yii::import('zii.widgets');

class NTriggerColumn extends CDataColumn
{
	/** 
	* The url of aciton where trigger will be executed
	* a URL or an action route that can be used to create a URL. See normalizeUrl for more details about how to specify this parameter.
	*/
	public $url = array('toggleAttribute');

	/** 
	* Value used to display respective to index 0 and 1
	*/
	public $displayValues = array('No', 'Yes');

	public function init()
	{
		parent::init();
		
		$this->headerHtmlOptions = CMap::mergeArray($this->headerHtmlOptions,array('width'=>'5'));
	}

	/**
	* Renders the data cell content.
	* This method evaluates {@link value} or {@link name} and renders the result.
	* @param integer $row the row number (zero-based)
	* @param mixed $data the data associated with the row
	*/
	protected function renderDataCellContent($row,$data)
	{
		// the class will be used for rendered link
		$linkClass = $this->id . '-link';

		// add model's id to ajax url
		$this->url['attr'] = $this->name;
		$this->url['id'] = $data->id;

		// refine value entered
		$value=$data->{$this->name}>=1?1:0;

		cs()->registerScript($this->id . '-script', "$('body').on('click', '.$linkClass', function(){\$.ajax({'async':false,'cache':false,'context':$(this),'type':'POST','url':$(this).attr('data-href'),'success':function(data){\$(this).html($(this).attr('val'+data));},'error':function(data){alert(data.responseText);}});return false;});");

		$link = CHtml::link($this->displayValues[$value], 'javascript:void(0)', array('class'=>$linkClass, 'val0'=>$this->displayValues[0],'val1'=>$this->displayValues[1],'data-href'=>CHtml::normalizeUrl($this->url)));
		echo CHtml::tag('div',array('class'=>'alc'),$link);
	}

}